import { Component } from "@angular/core"
import { NavController,App,AlertController,NavParams} from 'ionic-angular';

import { AppConfig } from '../../../app/app.config';

import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core'
@Component({
    selector: 'page-set',
    templateUrl: 'write.html',
})

export class WritePage{
    form = {
        content:'',
        star:0,
        pid:0,
        cid:0,
    }
    token:any;
    constructor(
        private alertCtrl:AlertController,
        private http:HttpClient,
        private storage: NativeStorage,
        private navParams:NavParams,
        private navCtl:NavController,
        private translate: TranslateService,
    ) {
        translate.setDefaultLang('en');
        let param = this.navParams.get('data')
        this.form.cid = param.id
        this.form.pid = param.info.id
        this.storage.getItem('token').then(token=>{
            this.token = token   
        })
    }
    ionViewDidEnter(){
        this.storage.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    goCust(){
        if(this.form.content.length<=500){
            this.http.post(AppConfig.getUrl()+'house/comment?token='+this.token, AppConfig.urlEcode(this.form),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( res => {
                if(res['code']===101){
                    this.alertb('success','Submit successful!');
                    this.navCtl.pop();
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
        }else{
            this.alertb('Error!','字数不能超过500');
        }
    }
}